//
// Created by 谢哲 on 2021/5/17.
//

#include <iostream>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <map>
using namespace std;

int vdata[1000009];

int vfind(int pos) {
    if (vdata[pos]==-1) return pos;
    return vdata[pos] = vfind(vdata[pos]);
}

void vmerge(int pos1, int pos2) {
    int p1 = vfind(pos1), p2 = vfind(pos2);
    if (p1!=p2) vdata[p1] = p2;
}

int main() {
    int t;
    cin >> t;

    for(;t;--t) {
        memset(vdata, -1, sizeof(vdata));
        map<int, int> mdata;
        vector<pair<int, int>> ddata;
        int n;
        cin >> n;
        for (;n;--n) {
            int i, j, e;
            cin >> i >> j >> e;
            if (mdata.find(i)==mdata.end()) {mdata[i] = mdata.size();}
            if (mdata.find(j)==mdata.end()) {mdata[j] = mdata.size();}
            i = mdata[i], j = mdata[j];
            if (e==1) vmerge(i, j);
            else ddata.emplace_back(make_pair(i, j));
        }
        bool flag = true;
        for(const auto& p: ddata) {
            if (vfind(p.first)==vfind(p.second)) {
                cout << "NO\n";
                flag = false;
                break;
            }
        }
        if (flag) cout << "YES\n";
    }

    return 0;
}
